#define _CRT_SECURE_NO_WARNINGS 1

#include <math.h>
struct ListNode {
	int val;
	struct ListNode* next;
};

struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB)
{
	if ((!headA) || (!headB))
		return NULL;
	struct ListNode* curA = headA, * curB = headB;
	int countA = 0, countB = 0;
	while (curA->next)
	{
		countA++;
		curA = curA->next;
	}
	while (curB->next)
	{
		countB++;
		curB = curB->next;
	}
	if (curA != curB)
		return NULL;
	int gap = abs(countA - countB);
	struct ListNode* longlist = headA, * shortlist = headB;
	if (countB > countA)
	{
		longlist = headB;
		shortlist = headA;
	}
	while (gap--)
	{
		longlist = longlist->next;
	}
	while (shortlist && longlist)
	{
		if (shortlist == longlist)
			return shortlist;
		shortlist = shortlist->next;
		longlist = longlist->next;
	}
	//return NULL;
}